<template>
  <div class="box">
    <van-nav-bar title="新增收货地址" left-arrow @click-left="onClickLeft" />
    <van-form>
      <van-field v-model="name" name="name" label="收货人" placeholder="请输入收货人姓名" />
      <van-field v-model="tel" name="tel" label="+86" placeholder="请输入收货人手机号" />
      <van-field v-model="result" is-link readonly name="address" label="所在地区" placeholder="点击选择省市区"
        @click="showArea = true" />
      <van-popup v-model:show="showArea" position="bottom">
        <van-area :area-list="areaList" @confirm="onConfirm" @cancel="showArea = false" />
      </van-popup>
      <div class="box1">
        <dl>
          <dt>
            详细地址
          </dt>
          <dd>
            <textarea v-model="text" name="2" id="" cols="25" rows="3"
              placeholder="详细地址:如街道、门牌号、楼栋号、单.元室等"></textarea>
               <van-icon @click="router.push('/ditu')" class="icon" name="location" />

          </dd>
        </dl>
      </div>
      <div class="box2">
        <van-button v-if="!route.query.item" round block type="primary" @click="onSubmit">
          保存地址
        </van-button>
        <van-button v-else round block type="primary" @click="Editlist">
          编辑地址
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { areaList } from '@vant/area-data';
import axios from 'axios'
import { showNotify } from 'vant';
import { useRouter, useRoute } from 'vue-router';
const onClickLeft = () => history.back();

const router = useRouter();
const route = useRoute()



const showArea = ref(false);
const onConfirm = ({ selectedOptions }) => {
  showArea.value = false;
  result.value = selectedOptions.map((item: any) => item.text).join(' ');
};

// 参数·                                                                                                         
const result = ref('');
const name = ref('');
const tel = ref('');
const text = ref('')
const id=ref('')


// 确定编辑
const Edit = async () => {
  const item = JSON.parse(route.query.item as any)
  if (item) {
    console.log('编辑')
    console.log(item)
    name.value = item.name
    tel.value = item.tel
    result.value = item.address
    text.value = item.text
    id.value = item.id
  } else {
    console.log('添加')
  }
}
Edit()


// 添加提交
const onSubmit = async () => {
  // 验证
  if (name.value == '' || tel.value == '' || result.value == '' || text.value == '') {
    showNotify({ type: 'warning', message: '请填写完整信息' });
    return;
  }
  // 也可以在此提交到后台
  const res = await axios.post('/api/v1/addressAdd', {
    name: name.value,
    tel: tel.value,
    address: result.value,
    text: text.value,
  })
  console.log(res)
  if (res.data.code == 200) {
    showNotify({ type: 'success', message: '添加成功' });
    router.push('/dizhi')
    console.log(res);
  } else {
    alert('提交失败')
  }

};

// 编辑后提交
const Editlist = async () => {
  await axios.put('/api/v1/addressEdit', {
    id:id.value,
    name: name.value,
    tel: tel.value,
    address: result.value,
    text: text.value
  })
  showNotify({ type: 'success', message: '编辑成功' });
  router.push('/dizhi')
}
const city=route.query.city
const dz=()=>{
  text.value=city
}
dz()








</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100vh;

  .box1 {
    width: 100%;
    height: 100px;
    overflow: hidden;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;

    dl {
      display: flex;
      align-items: center;
      width: 100%;
      height: 100px;

      dt {
        font-size: 15px;
        margin-left: 20px;
      }

      dd {
        margin-left: 30px;
        position: relative;
 .icon{
          position: absolute;
          top: 5px;
          right: -30px;
          font-size: 40px;
        }
        textarea {
          font-size: 14px;
          padding: 10px;
          border: none;
        }
      }
    }
  }

  .box2 {
    margin: 16px;
    position: fixed;
    bottom: 50px;
    left: 0;
    width: 90%
  }
  

}
</style>